//
//  StringUtil.swift
//  Extend
//
//  Created by 陆海升 on 2018/3/20.
//  Copyright © 2018年 敬信. All rights reserved.
//

public extension String {
    
    
    /// 空符四位加空格
    ///
    /// - Parameter str1:
    /// - Returns:
    public static func stringAddSpecialStr(str1: String) -> String {
        let str = str1.replacingOccurrences(of: " ", with: "")
        var doneTitle = ""
        var count: Int = 0
        for i in 0..<str.count {
            count += 1
            let start = str.index(str.startIndex, offsetBy: i)
            let end = str.index(str.startIndex, offsetBy: i + 1)
            doneTitle = doneTitle + (str.substring(with: start..<end))
            if count == 4 {
                doneTitle = "\(doneTitle) "
                count = 0
            }
        }
        return doneTitle
    }
    
    public static func StringToFloat(str:String) -> CGFloat {
        
        let string = str
        var cgFloat: CGFloat = 0
        
        if let doubleValue = Double(string)
        {
            cgFloat = CGFloat(doubleValue)
        }
        return cgFloat
    }
    
    public static func StringToInt(str:String)-> Int {
        
        let string = str
        var cgInt: Int = 0
        
        if let doubleValue = Double(string)
        {
            cgInt = Int(doubleValue)
        }
        return cgInt
    }
    //简化版Double(str)
    public static func StringToDouble(str:String)-> Double {
        
        let string = str
        var cgInt: Double = 0.0
        
        if let doubleValue = Double(string)
        {
            cgInt = (doubleValue)
        }
        return cgInt
    }
    
    public static func StringToData(str:String)-> Data {
        
        let strData = str.data(using: String.Encoding.utf8)
        
        return strData!
    }
}
